package com.epoint.mobile.demo.drag;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.GVIcon;
import com.epoint.mobileoa.bizlogic.SuperView;

/**
 * @author liyc
 * @time 2012-12-10 上午8:32:41
 * @annotation 
 */
public class WorkFlowDragView extends SuperView implements OnTouchListener{ 
	/** Called when the activity is first created. */
	private View selected_item = null;
	private int offset_x = 0;
	private int offset_y = 0;
	Boolean touchFlag = false;
	boolean dropFlag = false;
	LayoutParams imageParams;
	ImageView imageDrop,imgts;
	int crashX,crashY;
	Drawable dropDrawable,selectDrawable;
	Rect dropRect,selectRect;
	int topy,leftX,rightX,bottomY;
	int dropArray[];
	LinearLayout llLeft;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ViewGroup container = (ViewGroup) findViewById(R.id.container);
		imageDrop=(ImageView) findViewById(R.id.ImgDrop);    

		llLeft = (LinearLayout) findViewById(R.id.llLeft);

		//		LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//		final Button btn = (Button) mInflater.inflate(R.layout.btn_mainviewicon, null);
		//		btn.setText("同事");
		//		Drawable d = getResources().getDrawable(R.drawable.contackt_user);
		//		d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
		//		btn.setCompoundDrawables(null, d, null, null);
		//		llLeft.addView(btn);
		imgts = (ImageView)findViewById(R.id.img_ts);
		imgts.setOnTouchListener(this);

		container.setOnTouchListener(new View.OnTouchListener() 
		{
			public boolean onTouch(View v, MotionEvent event) 
			{
				if(touchFlag==true)
				{
					System.err.println("Display If  Part ::->"+touchFlag);
					switch (event.getActionMasked()) 
					{
					case MotionEvent.ACTION_DOWN :
						topy=imageDrop.getTop();
						leftX=imageDrop.getLeft();
						rightX=imageDrop.getRight();   
						bottomY=imageDrop.getBottom();
						System.err.println("Display Top-->"+topy);      
						System.err.println("Display Left-->"+leftX);
						System.err.println("Display Right-->"+rightX);
						System.err.println("Display Bottom-->"+bottomY);                
						//opRect.
						break;
					case MotionEvent.ACTION_MOVE:
						crashX=(int) event.getX();
						crashY=(int) event.getY();
						System.err.println("Display Here X Value-->"+crashX);
						System.err.println("Display Here Y Value-->"+crashY);
						int x = (int) event.getX() - offset_x;
						int y = (int) event.getY() - offset_y;                                          
						//int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
						//int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
						int w = getWindowManager().getDefaultDisplay().getWidth() - 50;
						int h = getWindowManager().getDefaultDisplay().getHeight() - 10;
						if (x > w)
							x = w;
						if (y > h)
							y = h;                      
						RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams(  RelativeLayout.LayoutParams.WRAP_CONTENT,   RelativeLayout.LayoutParams.WRAP_CONTENT));
						lp.setMargins(x, y, 0, 0);                

						//Drop Image Here                       
						if(crashX > leftX && crashX < rightX && crashY > topy && crashY < bottomY )                     
						{                           
							Drawable temp=selected_item.getBackground();                            
							imageDrop.setBackgroundDrawable(temp);
							imageDrop.setVisibility(View.VISIBLE);
							imageDrop.bringToFront();                           
							dropFlag=true;
							selected_item.setVisibility(View.INVISIBLE);
						}
						//Drop Image Here                       
						selected_item.setLayoutParams(lp);
						break;  
					case MotionEvent.ACTION_UP:
						//                      
						touchFlag=false;
						if(dropFlag==true)
						{
							dropFlag=false;
						}
						else
						{
							selected_item.setLayoutParams(imageParams);
						}                       
						break;
					default:
						break;
					}
				}else
				{
					System.err.println("Display Else Part ::->"+touchFlag);
				}               
				return true;
			}
		});
	}

	public boolean onTouch(View v, MotionEvent event) 
	{   
		switch (event.getActionMasked()) 
		{
		case MotionEvent.ACTION_DOWN:
			touchFlag=true;
			offset_x = (int) event.getX();
			offset_y = (int) event.getY();
			selected_item = v;
			imageParams=v.getLayoutParams();
			break;
		case MotionEvent.ACTION_UP:
			selected_item=null;
			touchFlag=false;
			break;
		default:
			break;
		}       
		return false;
	}

	@Override
	public void initControls(){

	}

	@Override
	public void refresh(Object... param) throws Exception {
	}

	@Override
	public int getLayoutInt() {
		return R.layout.workflowdragview;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}
}
